package view;

import javax.swing.*;


import java.awt.*;
import java.io.Serializable;

import control.*;

/**
 * The mainFrame of the game which contains the drawingboard,
 * menuPanel, currentPlayerPanel etc.
 * @author Nomel
 *
 */

public class BoardView extends JFrame implements Serializable
{
	private static final long serialVersionUID = 1L;

	private JPanel boardPanel, leftPanel, rightPanel;
	private PlayerListPanel playerListPanel;
	private CurrPlayerPanel playerPanel;
	private MenuPanel menuPanel;
	private DicePanel dicePanel;
	private Board drawPanel;
	
	public BoardView(GameControl gc)
	{
		super("ToWaJoJopoly");
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = this.getContentPane();
		
		boardPanel = new JPanel(new BorderLayout());
		menuPanel = new MenuPanel(gc);
		drawPanel = new Board(gc, gc.getFieldsControl().getBoard());
		dicePanel = new DicePanel();
		playerListPanel = new PlayerListPanel(gc.getPlayerControl());
		leftPanel = new JPanel(new BorderLayout());
		rightPanel = new JPanel(new BorderLayout());
		playerPanel =  new CurrPlayerPanel(gc.getPlayerControl());
		
		
		/* ---------------TEST-----------------------------------*/
		drawPanel.setPreferredSize(new Dimension(720, 720));
		/*menuPanel.setBackground(new Color(0x5b7fbb));
		playerListPanel.setBackground(new Color(0x5b7fbb));
		dicePanel.setBackground(new Color(0x5b7fbb));
		drawPanel.setBackground(new Color(0x5b7fbb));*/
		/* ------------------------------------------------------*/
		
		leftPanel.add(drawPanel);
		leftPanel.add(menuPanel, BorderLayout.SOUTH);
		
		rightPanel.add(playerPanel, BorderLayout.NORTH);
		rightPanel.add(dicePanel, BorderLayout.CENTER);
		rightPanel.add(playerListPanel, BorderLayout.SOUTH);
		
		boardPanel.add(leftPanel, BorderLayout.WEST);
		boardPanel.add(rightPanel, BorderLayout.EAST);
		c.add(boardPanel);
				
		this.addKeyListener(gc);
		this.setIconImage(ImageCreator.createImageIcon("Images/icon.png").getImage());
		//this.pack();
		this.setSize(1025, 725);
		this.setResizable(false);
		this.setVisible(true);
		this.requestFocus();
		
	}
		
	/*public static void main(String[] args)
	{
		BoardView b = new BoardView();
	}*/

	/**
	 * update the playerlist
	 */
	public void updatePlayerListView(IPlayerControl playerControl)
	{
		 playerListPanel.changeListData(playerControl);
	}
	
	/**
	 * update the current playerpanel
	 */
	public void updateCurrentPlayerView(IPlayerControl playerControl)
	{
		playerPanel.updateCurrentPlayer(playerControl);
	}
	
	/**
	 * disable the end turn button
	 * @deprecated
	 */
	public void disableEndTurn()
	{
		menuPanel.disableEndTurn();
	}
	
	/**
	 * Enables or disables buttons
	 * @param button the button to be enabled/disabled
	 * @param enabled boolean flag true or false
	 */
	public void enableButton(String button, boolean enabled)
	{
		if(button.equalsIgnoreCase("rollbutton"))
		{
			if(enabled == true)
				dicePanel.enableRollButton();
			else
				dicePanel.disableRollButton();
		}
		
		if(button.equalsIgnoreCase("endturnbutton"))
		{
			if(enabled == true)
				menuPanel.enableEndTurn();
			else
				menuPanel.disableEndTurn();
		}
		
		/*if(button.equalsIgnoreCase("menubuttons"))
		{
			if(enabled == true)
			;//TODO;
			else
				;//TODO
		}*/
	}
	
	/**
	 * updates the images of the dice
	 * @param left left dice
	 * @param right right dice
	 */
	public void setDiceImages(int left, int right)
	{
		dicePanel.setDiceImages(left, right);
	}
	
	/**
	 * update the image list (of the players)
	 */
	public void updateImageList()
	{
		drawPanel.updateImageList();
	}
	
	/**
	 * draw the houses
	 */
	public void drawHouses()
	{
		this.drawPanel.drawHouses();
		this.repaint();
	}
	
	/**
	 * click the rollbutton without clicking it graphically
	 */
	public void clickRollButton()
	{
		this.dicePanel.clickRollButton();
	}
	
	/**
	 * click the endturn button
	 */
	public void clickEndTurnButton()
	{
		this.menuPanel.clickEndTurnButton();
	}
	
	/**
	 * update the gametime
	 * @param gameTime the game to be updated
	 */
	public void updateGameTime(String gameTime)
	{
		this.drawPanel.updateTime(gameTime);
	}
}
